#!/usr/bin/env python
#
#Installs Ultimate Theme Manager. Lots of cp'ing. Some mkdir'ing. How do I get a list of users and their home directories?
#sharkbaitbobby+utm@gmail.com :)
import os
for x in os.environ.items():
	if x[0]=="USER":
		user = x[1]
	if x[0]=="HOME":
		home = x[1]

if user!="root":
	if not "-y" in sys.argv:
		run_sudo = raw_input("You are not currently root. This will only install for you, and not the whole system. Continue? (y/n) ")
		if run_sudo in ("n","N","no"):
			print "OK. Exiting. You may want to add sudo before \"python install.py\" to run as root."
			exit()
	
	os.system('mkdir %s/.utm' % home)
	os.system('mkdir %s/.utm/sources' % home)
	os.system('mkdir %s/.utm/sources/downloads' % home)
	#This section is for my default list. Feel free to change or remove it if you wish.
	os.system('mkdir %s/.utm/sources/09f911039d74e35bd84156c5635688c0' % home)
	os.system('cp imported.xml %s/.utm/sources/09f911039d74e35bd84156c5635688c0/imported.xml' % home)
	os.system('cp list.xml %s/.utm/sources/09f911039d74e35bd84156c5635688c0/list.xml' % home)
	os.system('echo 1 > %s/.utm/sources/09f911039d74e35bd84156c5635688c0/timestamp' % home)
	#End that section
	os.system('mkdir %s/.utm/sources/downloads/images' % home)
	os.system('cp images.xml %s/.utm/souces/downloads/images.xml' % home)
	os.system('mkdir %s/.utm/theme-files' % home)
	os.system('mkdir %s/.utm/themes' % home)
	os.system('mkdir %s/.utm/type-files' % home)
	os.system('mkdir %s/.utm/types' % home)
	os.system('cp logo.png %s/.utm/logo.png' % home)
	os.system('cp logo16.png %s/.utm/logo16.png' % home)
	os.system('cp logo24.png %s/.utm/logo24.png' % home)
	os.system('cp themes.xml %s/.utm/themes.xml' % home)
	os.system('cp types.xml %s/.utm/types.xml' % home)
	os.system('mkdir %s/.local/utm' % home)
	fp = open('%s/Desktop/ultimate-theme-manager.desktop' % home,'w+')
	fp.write('[Desktop Entry]\nVersion=1.0\nEncoding=UTF-8\nName=Ultimate Theme Manager\nType=Application\nComment=A theme manager for possibly any application.\nExec=%s/.local/utm/ultimate-theme-manager\nIcon=%s/.utm/logo.png\nCategories=GNOME;Utility;' % (home, home))
	fp.close()
	os.system('cp ultimate-theme-manager %s/.local/utm/ultimate-theme-manager' % home)
	os.system('chmod +x %s/.local/utm/ultimate-theme-manager' % home)

else:
	home_list = os.listdir('/home')
	os.system('mkdir /usr/share/ultimate-theme-manager')
	os.system('cp logo.png /usr/share/ultimate-theme-manager/logo.png')
	os.system('cp logo16.png /usr/share/ultimate-theme-manager/logo16.png')
	os.system('cp logo24.png /usr/share/ultimate-theme-manager/logo24.png')
	os.system('chmod -R 755 /usr/share/ultimate-theme-manager')
	for x in home_list:
		os.system('mkdir /home/%s/.utm' % x)
		os.system('ln -s /usr/share/ultimate-theme-manager/logo.png /home/%s/.utm/logo.png' % x)
		os.system('ln -s /usr/share/ultimate-theme-manager/logo24.png /home/%s/.utm/logo24.png' % x)
		os.system('ln -s /usr/share/ultimate-theme-manager/logo16.png /home/%s/.utm/logo16.png' % x)
		os.system('cp themes.xml /home/%s/.utm/themes.xml' % x)
		os.system('cp types.xml /home/%s/.utm/types.xml' % x)
		os.system('cp sources.xml /home/%s/.utm/sources.xml' % x)
		os.system('mkdir /home/%s/.utm/sources' % x)
		os.system('mkdir /home/%s/.utm/sources/downloads' % x)
		#This section is for my default list. Feel free to change or remove it if you wish.
		os.system('mkdir /home/%s/.utm/sources/09f911039d74e35bd84156c5635688c0' % x)
		os.system('cp imported.xml /home/%s/.utm/sources/09f911039d74e35bd84156c5635688c0/imported.xml' % x)
		os.system('cp list.xml /home/%s/.utm/sources/09f911039d74e35bd84156c5635688c0/list.xml' % x)
		os.system('echo 1 > /home/%s/.utm/sources/09f911039d74e35bd84156c5635688c0/timestamp' % x)
		#End that section
		os.system('mkdir /home/%s/.utm/sources/downloads/images' % x)
		os.system('cp images.xml /home/%s/.utm/souces/images.xml' % x)
		os.system('mkdir /home/%s/.utm/theme-files' % x)
		os.system('mkdir /home/%s/.utm/themes' % x)
		os.system('mkdir /home/%s/.utm/type-files' % x)
		os.system('mkdir /home/%s/.utm/types' % x)
		os.system('chown -hR %s /home/%s/.utm' % (x,x))
	
	os.system('cp logo.png /usr/share/pixmaps/ultimate-theme-manager.png')
	os.system('chmod 644 /usr/share/pixmaps/ultimate-theme-manager.png')
	os.system('cp ultimate-theme-manager /usr/bin/ultimate-theme-manager')
	os.system('chmod 755 /usr/bin/ultimate-theme-manager')
	os.system('cp ultimate-theme-manager.desktop /usr/share/applications/ultimate-theme-manager.desktop')
	os.system('chmod 644 /usr/share/applications/ultimate-theme-manager.desktop')

print "Done installing Ultimate Theme Manager! Use it by running ultimate-theme-manager or accessing it from the main menu."
